package com.rs.game.player.dialogues;

import com.rs.cache.loaders.ItemDefinitions;
import com.rs.game.item.Item;
import com.rs.game.player.Player;

public class BindItemD extends Dialogue {
	
	Item item;
	int bindTo;
	int oldId;
	
	public static int getBindedItemId(Item i) {
		String name = i.getDefinitions().getName().toLowerCase();
		for (int x = 15775;x < 16271;x++) {
			if (ItemDefinitions.getItemDefinitions(x).getName().toLowerCase().contains(name))
				return x;
		}
		return 15848;
	}
	
	public static int getOpenBind(Player p, Item item) {
		int ret = 100;
		if (p.getDungBinds() == null)
			p.setDungBinds(new Item[4]);
		for (int i = 0;i < p.getDungBinds().length;++i) {
			if (p.getDungBinds()[i] == null)
				return i;
			if (p.getDungBinds()[i].getId() == item.getId() && item.getDefinitions().isStackable())
				return i;
		}
		return ret;
	}
	
	@Override
	public void start() {
		item = (Item) parameters[0];
		oldId = item.getId();
		item.setId(getBindedItemId(item));
		bindTo = getOpenBind(player, item);
		int oldAmount = 0;
		if (bindTo != 100 && player.getDungBinds()[bindTo] != null)
			oldAmount = player.getDungBinds()[bindTo].getAmount();
		
		if (bindTo == 100) {
			sendDialogue("You already have 4 items bound. Choose one to replace with "+item.getDefinitions().getName()+".");
			stage = 1;
		} else if (player.getDungBinds()[bindTo] != null && player.getDungBinds()[bindTo].getId() == item.getId() && item.getDefinitions().isStackable()) {
			player.getDungBinds()[bindTo].setAmount(item.getAmount()+oldAmount);
			player.getInventory().addItem(item);
			sendDialogue("You bound the item to you.");
			stage = 5;
		} else {
			player.getDungBinds()[bindTo] = item;
			player.getInventory().deleteItem(oldId, 1);
			player.getInventory().addItem(item.getId(), 1);
			sendDialogue("You bound the item to you.");
			stage = 5;
		}
	}

	@Override
	public void run(int interfaceId, int componentId) {
		if (stage == 1) {
			sendOptionsDialogue("Which item to replace with "+item.getDefinitions().getName()+"?", 
					player.getDungBinds()[0].getDefinitions().getName(), 
					player.getDungBinds()[1].getDefinitions().getName(),
					player.getDungBinds()[2].getDefinitions().getName(),
					"None I change my mind!");
			stage = 2;
		} else if (stage == 2) {
			switch(componentId) {
			case 11:
				player.getDungBinds()[0] = item;
				player.getInventory().deleteItem(oldId, 1);
				player.getInventory().addItem(item.getId(), 1);
				break;
			case 13:
				player.getDungBinds()[1] = item;
				player.getInventory().deleteItem(oldId, 1);
				player.getInventory().addItem(item.getId(), 1);
				break;
			case 14:
				player.getDungBinds()[2] = item;
				player.getInventory().deleteItem(oldId, 1);
				player.getInventory().addItem(item.getId(), 1);
				break;
			default:
				break;
			}
			end();
		} else if (stage == 5) {
			end();
		}
	}

	@Override
	public void finish() {

	}
}
